#!/usr/bin/python
# Author: M. Fauzilkamil Zainuddin
# Author URI: http://coderstalk.blogspot.com

# read the file /proc/net/dev
f = open('/proc/net/dev','r')

# put the content to list
ifacelist = f.read().split('\n') 

# close the file
f.close()

# remove 2 lines header
ifacelist.pop(0)
ifacelist.pop(0)

def getifip(ifn):
    # print ip
    import socket, fcntl, struct

    sck = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(sck.fileno(),0x8915,struct.pack('256s', ifn[:15]))[20:24])
    

# loop to check each line
for line in ifacelist:

    ifacedata = line.replace(' ','').split(':')
    #print ifacedata

    # check the data have 2 elements
    if len(ifacedata) == 2:

        # check the interface is up (Transmit/Receive data)
        if int(ifacedata[1]) > 0:

            # print the interface
            print ifacedata[0]
            print getifip(ifacedata[0])
            

